package is.api.demo.isgateway.utils;

import is.api.demo.isgateway.support.RequestHeader;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Objects;

/**********************************************************************
 * &lt;p&gt;文件名：IgGateWayMediaTypeUtils.java &lt;/p&gt;
 * &lt;p&gt;文件描述：(描述该文件做什么) 
 * @project_name：rest-api-demo
 * @author zengshunyao
 * @create 2025/11/27 19:04
 * @history
 * @department：政务事业部
 * Copyright ChengDu Funi Cloud Code Technology Development CO.,LTD 2014 
 *                    All Rights Reserved.
 */
public class IsGateWayMediaTypeUtils {
    /**
     * @param allHeaders
     * @return
     */
    private static String getContentType(Header[] allHeaders) {
        final String value = Arrays.stream(allHeaders)
                .filter(e -> Objects.equals(e.getName(), RequestHeader.CONTENT_TYPE))
                .findFirst()
                .map(NameValuePair::getValue)
                .orElse(null);
        System.out.println("ContentType=" + value);
        return value;
    }

    /**
     * 返回格式是否为文件流
     *
     * @param allHeaders
     * @return
     */
    public static boolean isApplicationOctetStream(Header[] allHeaders) {
        final String value = getContentType(allHeaders);
        if (!StringUtils.hasText(value)) {
            return false;
        }
        if (value.contains(MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
            return true;
        }
        return false;
    }

    /**
     * 返回格式是否为json
     *
     * @param allHeaders
     * @return
     */
    public static boolean isApplicationJson(Header[] allHeaders) {
        final String value = getContentType(allHeaders);
        if (!StringUtils.hasText(value)) {
            return false;
        }
        if (value.contains(MediaType.APPLICATION_JSON_VALUE)) {
            return true;
        }
        return false;
    }
}
